package br.com.fpf.pontoapp.model.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.Minutes;

/**
 * The Class DateUtil.
 */
public class DateUtil {

	/** The formatador. */
	static SimpleDateFormat formatador = new SimpleDateFormat();

	/** The Constant PATTERN_DATA_DIA_MES_ANO. */
	public static final String PATTERN_DATA_DIA_MES_ANO = "dd/MM/yyyy";

	/**
	 * Gets the string as date.
	 * 
	 * @param data
	 *            the data
	 * @param pattern
	 *            the pattern
	 * @return the string as date
	 */
	public static Date getStringAsDate(String data, String pattern) {
		if (data != null) {
			DateUtil.formatador.applyPattern(pattern);
			try {
				return DateUtil.formatador.parse(data);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * Gets the string as date.
	 * 
	 * @param data
	 *            the data
	 * @return the string as date
	 */
	public static Date getStringAsDate(String data) {
		return DateUtil
				.getStringAsDate(data, DateUtil.PATTERN_DATA_DIA_MES_ANO);
	}

	// TODO analisar
	/**
	 * Formata data.
	 * 
	 * @param data
	 *            the data
	 * @return the date
	 */
	public static Date formataData(String data) {
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		Calendar calendar = Calendar.getInstance();

		try {
			if (!data.equals("")) {
				calendar.set(Calendar.HOUR_OF_DAY, format.parse(data)
						.getHours());
				calendar.set(Calendar.MINUTE, format.parse(data).getMinutes());
			} else {
				return null;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return calendar.getTime();
	}

	// TODO analisar
	/**
	 * Calcula intervalo em horas.
	 * 
	 * @param dataMenor
	 *            the data menor
	 * @param dataMaior
	 *            the data maior
	 * @return the date
	 */
	public static Date calculaIntervaloEmHoras(Date dataMenor, Date dataMaior) {
		if (dataMenor != null && dataMaior != null) {
			DateTime dataMenorJodaTime = new DateTime(dataMenor.getTime());
			DateTime dataMaiorJodaTime = new DateTime(dataMaior.getTime());
			Minutes minutos = Minutes.minutesBetween(dataMenorJodaTime,
					dataMaiorJodaTime);

			Double horasMinutosMais = (double) (minutos.getMinutes() / 60.0);
			float minutosMais = (float) ((horasMinutosMais - horasMinutosMais
					.intValue()) * 60.0);
			int horaMais = horasMinutosMais.intValue();

			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.HOUR_OF_DAY, horaMais);
			calendar.set(Calendar.MINUTE, Float.valueOf(minutosMais).intValue());

			return calendar.getTime();
		}
		return null;

	}

	// TODO analisar
	/**
	 * Soma datas.
	 * 
	 * @param data1
	 *            the data1
	 * @param data2
	 *            the data2
	 * @return the date
	 */
	public static Date somaDatas(Date data1, Date data2) {
		DateTime data1JodaTime = new DateTime(data1.getTime());
		DateTime data2JodaTime = new DateTime(data2.getTime());
		data1JodaTime = data1JodaTime.plusMinutes(data2JodaTime
				.getMinuteOfHour());
		data1JodaTime = data1JodaTime.plusHours(data2JodaTime.getHourOfDay());

		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, data1JodaTime.getHourOfDay());
		calendar.set(Calendar.MINUTE, data1JodaTime.getMinuteOfHour());
		return calendar.getTime();

	}

	// TODO analisar
	/**
	 * Subtrai datas.
	 * 
	 * @param data1
	 *            the data1
	 * @param data2
	 *            the data2
	 * @return the date
	 */
	public static Date subtraiDatas(Date data1, Date data2) {
		DateTime data1JodaTime = new DateTime(data1.getTime());
		DateTime data2JodaTime = new DateTime(data2.getTime());
		Calendar calendar = Calendar.getInstance();

		if (data1.after(data2)) {
			data1JodaTime = data1JodaTime.minusMinutes(data2JodaTime
					.getMinuteOfHour());
			data1JodaTime = data1JodaTime.minusHours(data2JodaTime
					.getHourOfDay());

			calendar.set(Calendar.HOUR_OF_DAY, data1JodaTime.getHourOfDay());
			calendar.set(Calendar.MINUTE, data1JodaTime.getMinuteOfHour());

			return calendar.getTime();
		}
		data2JodaTime = data2JodaTime.minusMinutes(data1JodaTime
				.getMinuteOfHour());
		data2JodaTime = data2JodaTime.minusHours(data1JodaTime.getHourOfDay());

		calendar.set(Calendar.HOUR_OF_DAY, data2JodaTime.getHourOfDay());
		calendar.set(Calendar.MINUTE, data2JodaTime.getMinuteOfHour());

		return calendar.getTime();

	}

	// TODO analisar
	/**
	 * Criar data.
	 * 
	 * @param dia
	 *            the dia
	 * @param mes
	 *            the mes
	 * @param ano
	 *            the ano
	 * @param hora
	 *            the hora
	 * @param minuto
	 *            the minuto
	 * @return the date
	 */
	public static Date criarData(Integer dia, Integer mes, Integer ano,
			Integer hora, Integer minuto) {
		Calendar calenda = Calendar.getInstance();
		calenda.set(Calendar.YEAR, ano);
		calenda.set(Calendar.MONTH, mes);
		calenda.set(Calendar.DAY_OF_MONTH, dia);
		calenda.set(Calendar.HOUR_OF_DAY, hora);
		calenda.set(Calendar.MINUTE, minuto);
		calenda.set(Calendar.SECOND, 0);

		return calenda.getTime();
	}
}
